package com.wnhz.work.jy.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.solidfire.gson.Gson;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.activity.LogisticsActivity;
import com.wnhz.work.jy.activity.MyOrderActivity;
import com.wnhz.work.jy.activity.OrderDetailsActivity;
import com.wnhz.work.jy.adapter.AllOrderAdapter;
import com.wnhz.work.jy.bean.OrderBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * ----待发货
 */
@ContentView(R.layout.fragment_all_order)
public class WaitDeliverFragment extends OrderFragment implements View.OnClickListener {
    private View view;
    @ViewInject(R.id.lv_all_order)
    private ListView lv_all_order;
    private AllOrderAdapter mAdapter;

    private List<OrderBean> orderList = new ArrayList<>();



    @ViewInject(R.id.include_occupied)
    private View include_occupied;
    @ViewInject(R.id.blank_img)
    private ImageView blank_img;
    @ViewInject(R.id.blank_tv)
    private TextView blank_tv;
    private MyOrderActivity myOrderActivity;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        /*view = inflater.inflate(R.layout.fragment_all_order, container, false);
        initData();
        initView();*/
        myOrderActivity = (MyOrderActivity) getActivity();
        return x.view().inject(this,inflater,container);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        getData();
        setBlank();
    }

    private void setBlank(){
        if (orderList.size() == 0){
            blank_tv.setText("还没有该状态下的订单哦~");
            blank_img.setImageResource(R.mipmap.ic_kongdingdan);
            include_occupied.setVisibility(View.VISIBLE);
        }else {
            include_occupied.setVisibility(View.GONE);
        }
    }



    private void initView() {
        //lv_all_order = (ListView) view.findViewById(R.id.lv_all_order);

        mAdapter = new AllOrderAdapter(getActivity(), orderList, this);
        lv_all_order.setAdapter(mAdapter);

    }

    @Override
    public void onClick(View v) {
        int i = (int) v.getTag();
        switch (v.getId()) {
            case R.id.view:
            case R.id.lay:
                Intent intent = new Intent(getActivity(), OrderDetailsActivity.class);
                intent.putExtra("orderId",orderList.get(i).getOrderId());
                startActivity(intent);
                break;
            //取消订单
            case R.id.tv_logistics:
                startActivity(new Intent(getActivity(), LogisticsActivity.class).putExtra("orderId",orderList.get(i).getOrderId()).putExtra("OrderState",orderList.get(i).getOrderState()));
                break;

            //评价
            case R.id.tv_evaluate:
                myOrderActivity.cancalOrder(orderList.get(i).getOrderId(),2,"2");
                break;

            default:
                break;
        }
        mAdapter.notifyDataSetChanged();
    }

    private static final String TAG = "WaitDeliverFragment";
    public void getData(){
        Map<String,Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        map.put("type","1");
        myOrderActivity.showDialog();
        XUtil.Post(ConfigUtil.ORDER_LIST,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: "+result );
                try {
                    JSONObject json = new JSONObject(result);
                    String re = json.optString("result");
                    orderList.clear();
                    if ("1".equals(re)){
                        Gson gson = new Gson();
                        JSONArray array = json.optJSONArray("order");
                        if (array != null){
                            for (int i = 0; i < array.length(); i++) {
                                OrderBean b = new OrderBean();
                                b = gson.fromJson(array.optJSONObject(i).toString(),OrderBean.class);
                                orderList.add(b);
                            }

                        }
                    }
                    mAdapter.notifyDataSetChanged();
                    setBlank();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                myOrderActivity.closeDialog();
            }
        });
    }
}
